﻿using System;
using UnityEngine;
using BridgeShips.InputSystem;
using BridgeShips.AI;


namespace BridgeShips
{
    /// <summary>
    /// Handles the player input, and feeds it to the other components.
    /// </summary>
    public class PlayerInputHandler : PlayerBehaviour
    {
        [SerializeField]
        private bool m_ShowControls;

        private InputManager m_Input;

        private int m_MouseRightPressCount = 0;
        private int m_KeyTPressCount = 0;
        private int m_KeyMPressCount = 0;
        private float m_IntervalTime = 0.0f;

        public static Activity curActivity;//当前打开的面板
        private void Awake()
        {
            if (GameController.InputManager)
                m_Input = GameController.InputManager;
            else
                enabled = false;

        }

        private void OnGUI()
        {
            if (!m_ShowControls)
                return;

            Rect rect = new Rect(Screen.width - 8f - 150f, 32f, 150f, 30f);

            //GUI.Label(rect, "Unlock cursor - Escape");

            //rect.y = rect.yMax + 4f;
            //GUI.Label(rect, "Inventory - TAB");

            //rect.y = rect.yMax + 4f;
            //GUI.Label(rect, "Move - WASD");

            //rect.y = rect.yMax + 4f;
            //GUI.Label(rect, "Attack - Left Click");

            //rect.y = rect.yMax + 4f;
            //GUI.Label(rect, "Aim - Right Click");

            //rect.y = rect.yMax + 4f;
            //GUI.Label(rect, "Climate - C");

            //rect.y = rect.yMax + 4f;
            //GUI.Label(rect, "Objects - Left Shift");

            //rect.y = rect.yMax + 4f;
            //GUI.Label(rect, "Interact - E");

            //rect.y = rect.yMax + 4f;
            //GUI.Label(rect, "Rotate - Arrows");

            //rect.y = rect.yMax + 4f;
            //GUI.Label(rect, "Wheel - Right Click");

            //rect.y = rect.yMax + 4f;
            if (GUI.Button(rect, "Quit!"))
                Application.Quit();
        }

        private void Update()
        {
            // Inventory.
            if (!InventoryController.Instance.IsClosed && m_Input.GetButtonDown("Close Inventory"))
                InventoryController.Instance.SetState.Try(ET.InventoryState.Closed);
            if (InventoryController.Instance.IsClosed && m_Input.GetButtonDown("Open Inventory"))
                InventoryController.Instance.SetState.Try(ET.InventoryState.Normal);

            var rotateObjectAxis = m_Input.GetAxisRaw("Rotate Object");
            if (Mathf.Abs(rotateObjectAxis) > 0f)
                Player.RotateObject.Try(rotateObjectAxis);

            if (m_Input.GetButtonDown("Select Buildable"))
            {
                m_IntervalTime = (m_MouseRightPressCount == 0) ? Time.time : m_IntervalTime;
                if (Time.time - m_IntervalTime >= 0.5f)
                    m_MouseRightPressCount = 0;
                else
                    m_MouseRightPressCount++;

                if (m_MouseRightPressCount == 2)
                {
                    if (!Player.SelectBuildable.Active) {
                        Player.SelectBuildable.TryStart();
                        if (curActivity != null) {
                            curActivity.TryStop();
                        }
                        curActivity = Player.SelectBuildable;                        
                    }
                    else {
                        Player.SelectBuildable.TryStop();
                        curActivity = null;
                    }
                        
                }
                return;
            }

            //plane list 
            if (m_Input.GetButtonDown( "Objects" )) {
                m_IntervalTime = (m_KeyTPressCount == 0) ? Time.time : m_IntervalTime;
                if (Time.time - m_IntervalTime >= 0.5f)
                    m_KeyTPressCount = 0;
                else
                    m_KeyTPressCount++;

                if (m_KeyTPressCount == 1) {
                    if (!Player.Objects.Active)
                        Player.Objects.TryStart();
                    else
                        Player.Objects.TryStop();
                }
                return;
            }

            // Climate UI
            if (m_Input.GetButtonDown( "Climate" )) {
                Debug.LogError(112);
                m_IntervalTime = (m_KeyMPressCount == 0) ? Time.time : m_IntervalTime;
                if (Time.time - m_IntervalTime >= 0.5f)
                    m_KeyMPressCount = 0;
                else
                    m_KeyMPressCount++;

                if (m_KeyMPressCount == 1) {
                    if (!Player.Climate.Active)
                        Player.Climate.TryStart();
                    else
                        Player.Climate.TryStop();
                }
                return;
            }

            // Climate UI
            if (m_Input.GetButtonDown( "LineCharts" )) {
                m_IntervalTime = (m_KeyMPressCount == 0) ? Time.time : m_IntervalTime;
                if (Time.time - m_IntervalTime >= 0.5f)
                    m_KeyMPressCount = 0;
                else
                    m_KeyMPressCount++;

                if (m_KeyMPressCount == 1) {
                    if (!Player.LineCharts.Active) {
                        Player.LineCharts.TryStart();
                        if (curActivity != null) {
                            curActivity.TryStop();
                        }
                        curActivity = Player.LineCharts;
                    }
                    else {
                        Player.LineCharts.TryStop();
                        curActivity = null;
                    }       
                }
                return;
            }

            //Set UI
            if (m_Input.GetButtonDown( "Set" )) {
                m_IntervalTime = (m_KeyMPressCount == 0) ? Time.time : m_IntervalTime;
                if (Time.time - m_IntervalTime >= 0.5f)
                    m_KeyMPressCount = 0;
                else
                    m_KeyMPressCount++;

                if (m_KeyMPressCount == 1) {
                    if (!Player.Set.Active) {
                        Player.Set.TryStart();
                        if (curActivity != null) {
                            curActivity.TryStop();
                        }
                        curActivity = Player.Set;
                    }
                    else {
                        Player.Set.TryStop();
                        curActivity = null;
                    }
                        
                }
                return;
            }

            //Help UI
            if (m_Input.GetButtonDown( "Help" )) {
                m_IntervalTime = (m_KeyMPressCount == 0) ? Time.time : m_IntervalTime;
                if (Time.time - m_IntervalTime >= 0.5f)
                    m_KeyMPressCount = 0;
                else
                    m_KeyMPressCount++;

                if (m_KeyMPressCount == 1) {
                    if (!Player.Help.Active) {
                        Player.Help.TryStart();
                        if (curActivity != null) {
                            curActivity.TryStop();
                        }
                        curActivity = Player.Help;
                    }
                    else {
                        Player.Help.TryStop();
                        curActivity = null;
                    }
                        
                }
                return;
            }

            //Information UI
            if (m_Input.GetButtonDown( "Information" )) {
                m_IntervalTime = (m_KeyMPressCount == 0) ? Time.time : m_IntervalTime;
                if (Time.time - m_IntervalTime >= 0.5f)
                    m_KeyMPressCount = 0;
                else
                    m_KeyMPressCount++;

                if (m_KeyMPressCount == 1) {
                    if (!Player.Information.Active) {
                        Player.Information.TryStart();
                        if (curActivity != null) {
                            curActivity.TryStop();
                        }
                        curActivity = Player.Information;
                    }
                    else {
                        Player.Information.TryStop();
                        curActivity = null;
                    }         
                }
                return;
            }
            //ChooseShip UI
            if (m_Input.GetButtonDown("ChooseShip"))
            {
                m_IntervalTime = (m_KeyMPressCount == 0) ? Time.time : m_IntervalTime;
                if (Time.time - m_IntervalTime >= 0.5f)
                    m_KeyMPressCount = 0;
                else
                    m_KeyMPressCount++;

                if (m_KeyMPressCount == 1)
                {
                    if (!Player.ChooseShip.Active)
                    {
                        Player.ChooseShip.TryStart();
                        if (curActivity != null)
                        {
                            curActivity.TryStop();
                        }
                        curActivity = Player.ChooseShip;
                    }
                    else
                    {
                        Player.ChooseShip.TryStop();
                        curActivity = null;
                    }
                }
                return;
            }
            

           // Map UI
           //if (m_Input.GetButtonDown("Map"))
           //{
           //    m_IntervalTime = (m_KeyMPressCount == 0) ? Time.time : m_IntervalTime;
           //    if (Time.time - m_IntervalTime >= 0.5f)
           //        m_KeyMPressCount = 0;
           //    else
           //        m_KeyMPressCount++;

            //    if (m_KeyMPressCount == 1)
            //    {
            //        if (!Player.Map.Active)
            //        {
            //            Player.Map.TryStart();
            //            if (curActivity != null)
            //            {
            //                curActivity.TryStop();
            //            }
            //            curActivity = Player.Map;
            //        }
            //        else
            //        {
            //            Player.Map.TryStop();
            //            curActivity = null;
            //        }
            //    }
            //    return;
            //}
            if (InventoryController.Instance.IsClosed && Player.ViewLocked.Is(false))
            {
                // Movement.
                Vector2 moveInput = new Vector2(m_Input.GetAxis("Horizontal"), m_Input.GetAxis("Vertical"));
                Player.MovementInput.Set(moveInput);

                // Look.
                Player.LookInput.Set(new Vector2(m_Input.GetAxisRaw("Mouse X"), m_Input.GetAxisRaw("Mouse Y")));

                // Interact once.
                if (m_Input.GetButtonDown("Interact"))
                    Player.InteractOnce.Try();

                // Interact continuously.
                Player.InteractContinuously.Set(m_Input.GetButton("Interact"));

                // LineCharts.
                if (m_Input.GetButtonDown("LineCharts"))
                    Player.LineCharts.TryStart();

                // Run.
                bool sprintButtonHeld = m_Input.GetButton( "Objects" );
                bool canStartSprinting = Player.IsGrounded.Get() && Player.MovementInput.Get().y > 0f;


                // Aim.
                if (m_Input.GetButtonDown("Aim"))
                    Player.Aim.TryStart();
                else if (m_Input.GetButtonUp("Aim"))
                    Player.Aim.ForceStop();

                if (m_Input.GetButtonDown("Place Object"))
                {
                    if (Player.CanShowObjectPreview.Is(true))
                        Player.PlaceObject.Try();
                }
                else
                    Player.CanShowObjectPreview.Set(true);
            }
            else
            {
                // Movement.
                Player.MovementInput.Set(Vector2.zero);

                // Look.
                Player.LookInput.Set(Vector2.zero);
            }

            var scrollWheelValue = m_Input.GetAxis("Mouse ScrollWheel");
            Player.ScrollValue.Set(scrollWheelValue);
        }

        private void OnSucceded_PlaceObject()
        {
            Player.CanShowObjectPreview.Set(false);
        }
    }
}
